package com.yami.shop.api.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yami.shop.bean.model.BusinessNft;
import com.yami.shop.bean.model.User;
import com.yami.shop.common.response.ServerResponseEntity;
import com.yami.shop.service.BusinessNftService;
import com.yami.shop.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/business/nft")
@Tag(name = "同步增加持仓数据")
@Slf4j
@AllArgsConstructor
public class BusinessNftSyncDataController {

    private final BusinessNftService businessNftService;

    private final UserService userService;

    @PostMapping("/syncData")
    @Operation(summary = "保存同步数据", description = "保存同步数据")
    public ServerResponseEntity<Void> saveSyncData(@RequestBody List<BusinessNft> list){
        if (list.isEmpty()) {
            return ServerResponseEntity.showFailMsg("无数据");
        }
        log.info("====待增加持仓接收数据：{}====", list.toString());
        try {
            //校验外部id是否重复
            List<String> outIds = list.stream().map(BusinessNft::getOutId).collect(Collectors.toList());
            List<BusinessNft> list1 = businessNftService.list(Wrappers.lambdaQuery(BusinessNft.class).in(BusinessNft::getBusinessType, 10,11).in(BusinessNft::getOutId, outIds).select(BusinessNft::getOutId));
            List<String> outIds1 = list1.stream().map(BusinessNft::getOutId).collect(Collectors.toList());

            Map<String, List<BusinessNft>> collect = list.stream().collect(Collectors.groupingBy(BusinessNft::getType));
            List<BusinessNft> lsit = new ArrayList<>();
            for (String key : collect.keySet()) {
                if("1".equals(key)){
                    //购买
                    List<BusinessNft> businessNfts = collect.get(key);
                    Set<String> idCards = businessNfts.stream().map(BusinessNft::getIdCard).collect(Collectors.toSet());
                    List<User> userList = userService.list(Wrappers.lambdaQuery(User.class).eq(User::getStatus,1).in(User::getIdNumber, idCards));
                    Map<String, String> userMap = userList.stream().collect(Collectors.toMap(User::getIdNumber, User::getUserId));
                    for(Iterator<BusinessNft> iterator = businessNfts.iterator(); iterator.hasNext(); ){
                        BusinessNft next = iterator.next();
                        //去除业务id重复的
                        if(!outIds1.contains(next.getOutId())){
                            next.setBusinessType(10);
                            next.setUserId(userMap.get(next.getIdCard()));
                            next.setStatus(0);
                            lsit.add(next);
                        }
                    }
                }else{
                    //分享 领取分享即
                    List<BusinessNft> businessNfts = collect.get(key);
                    Set<String> idCards = businessNfts.stream().map(BusinessNft::getIdCard).collect(Collectors.toSet());
                    Set<String> idFromCards = businessNfts.stream().map(BusinessNft::getFromIdCard).collect(Collectors.toSet());
                    idCards.addAll(idFromCards);
                    List<User> userList = userService.list(Wrappers.lambdaQuery(User.class).eq(User::getStatus,1).in(User::getIdNumber, idCards));
                    Map<String, String> userMap = userList.stream().collect(Collectors.toMap(User::getIdNumber, User::getUserId));
                    for(Iterator<BusinessNft> iterator = businessNfts.iterator(); iterator.hasNext(); ){
                        BusinessNft next = iterator.next();
                        //去除业务id重复的
                        if(!outIds1.contains(next.getOutId())){
                            next.setBusinessType(11);
                            next.setUserId(userMap.get(next.getIdCard()));
                            next.setFromUserId(userMap.get(next.getFromIdCard()));
                            next.setStatus(0);
                            lsit.add(next);
                        }
                    }
                }
                businessNftService.saveBatch(lsit);
            }
            return ServerResponseEntity.success();
        } catch (Exception e) {
            return ServerResponseEntity.showFailMsg(e.getMessage());
        }
    }

}
